package cn.edu.zjut.mina.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;

import cn.edu.zjut.mina.bean.GameSummarize;
import cn.edu.zjut.mina.bean.MyPage;
import cn.edu.zjut.mina.bean.PlayerCredit;
import cn.edu.zjut.mina.po.EveryGameResult;
import cn.edu.zjut.mina.po.Total;
import cn.edu.zjut.mina.po.TotalGameResult;
import cn.edu.zjut.mina.po.User;
import cn.edu.zjut.mina.po.UserInfo;
import cn.edu.zjut.mina.repository.TotalGameResultRepository;
import cn.edu.zjut.mina.repository.TotalRepository;
import cn.edu.zjut.mina.repository.UserInfoRepository;
import cn.edu.zjut.mina.service.IInquireService;

@Service("inquireService")
public class InquireServiceImpl implements IInquireService {

	@Autowired
	private TotalGameResultRepository totalGameResultRepository;

	@Autowired
	private TotalRepository totalRepository;

	@Autowired
	private UserInfoRepository userInfoRepository;

	/**
	 * 分页请求所有内容
	 */
	public String inquireTotal(String userId, int page, int pagenum) {
		// TODO Auto-generated method stub
		// totalGameResultRepository

		// Sort sort = new Sort(Direction.DESC, "autoid");
		Pageable pageable = new PageRequest(page, pagenum);
		/**
		 * 不能直接把page给return到客户端，会出现无限解析的问题(循环解析)
		 */

		Page<TotalGameResult> totalList = totalGameResultRepository
				.findTotalGameResultByUserId(userId, pageable);
		/**
		 * 不能直接把page给return到客户端，会出现无限解析的问题(循环解析)
		 */
		System.out
				.println(JSON.toJSONString(totalList) + " bbbbbbbbbbbbbbbbbb");

		return JSON.toJSONString(totalList);
	}

	/**
	 * 分页请求战绩综述
	 */
	public String inquireSummarize(String userId, int page, int pagenum) {
		Pageable pageable = new PageRequest(page, pagenum);
		/**
		 * 不能直接把page给return到客户端，会出现无限解析的问题(循环解析)
		 */

		Page<TotalGameResult> totalList = totalGameResultRepository
				.findTotalGameResultByUserId(userId, pageable);

		// 为Page赋值
		MyPage<GameSummarize> gameSummarizePage = new MyPage<GameSummarize>();
		gameSummarizePage.setFirst(totalList.isFirst());
		gameSummarizePage.setLast(totalList.isLast());
		gameSummarizePage.setNumber(totalList.getNumber());
		gameSummarizePage.setNumberOfElements(totalList.getNumberOfElements());
		gameSummarizePage.setSize(totalList.getSize());
		gameSummarizePage.setTotalElements(totalList.getTotalElements());
		gameSummarizePage.setTotalPages(totalList.getTotalPages());

		List<TotalGameResult> totalGameResultList = totalList.getContent();// 获取游戏结果
		List<GameSummarize> gameSummarizeList = new ArrayList<GameSummarize>();
		for (int i = 0; i < totalGameResultList.size(); i++) {// 有多少局
			TotalGameResult totalGameResult = totalGameResultList.get(i);
			// 为每一局游戏赋值
			GameSummarize gameSummarize = new GameSummarize();
			gameSummarize.actual_number_of_games = totalGameResult
					.getActual_number_of_games();
			gameSummarize.beginTime = totalGameResult.getBeginTime();
			gameSummarize.endTime = totalGameResult.getEndTime();
			gameSummarize.createRoomPlayerId = totalGameResult
					.getCreateRoomPlayerId();
			gameSummarize.criticalOfTwoTimes = totalGameResult
					.getCriticalOfTwoTimes();
			gameSummarize.deductRoomCard = totalGameResult.getDeductRoomCard();
			gameSummarize.lzxs = totalGameResult.getLzxs();
			gameSummarize.participation = totalGameResult.getParticipation();
			gameSummarize.ratio = totalGameResult.getRatio();
			gameSummarize.roomId = totalGameResult.getRoomId();
			gameSummarize.totalGameResultId = totalGameResult
					.getTotalGameResultId();

			List<PlayerCredit> playerCreditList = new ArrayList<PlayerCredit>();
			for (int j = 0; j < totalGameResult.getEveryGameResult().get(i)
					.getPlayerBattleInfos().size(); j++) {// 有几个玩家
				int credit = 0;// 积分统计
				PlayerCredit playerCredit = new PlayerCredit();
				playerCredit.userId = totalGameResult.getEveryGameResult()
						.get(i).getPlayerBattleInfos().get(j).getUserId();
				UserInfo user = userInfoRepository
						.findByUserId(playerCredit.userId);
				playerCredit.nickname = user.getNickName();
				playerCredit.headimgurl = user.getHeadimgurl();
				for (int k = 0; k < totalGameResult.getEveryGameResult().size(); k++) {// 每局游戏有几次
					credit += totalGameResult.getEveryGameResult().get(k)
							.getPlayerBattleInfos().get(j).getCredit();
				}
				playerCredit.credit = credit;
				playerCreditList.add(playerCredit);
			}

			gameSummarize.PlayerCreditList = playerCreditList;
			gameSummarizeList.add(gameSummarize);

		}

		gameSummarizePage.setContent(gameSummarizeList);

		System.out.println(JSON.toJSONString(gameSummarizePage)
				+ " aaaaaaaaaaaaaaaaaaaa");

		return JSON.toJSONString(gameSummarizePage);
	}

	/**
	 * 请求每条战绩
	 */
	public String inquireItem(int totalGameResultId) {
		// TODO Auto-generated method stub
		TotalGameResult totalGameResult = totalGameResultRepository.findByTotalGameResultId(totalGameResultId);
		
		return JSON.toJSONString(totalGameResult);
	}

}
